<?php
namespace Users\Admin;

use \VSF\Mvc\Controller\AbstractActionController;
use \Zend\View\Model\ViewModel;

use \VSF\Form\Form;

use Users\Entity\Users;

class UsersController extends AbstractActionController
{
	
	
	public function indexAction()
	{
		$viewParams = array();
		$listUsers =  $this->getEntityManager()->getRepository('Users\Entity\Users')->findAll();
		$viewParams['users'] = $listUsers;
		
		
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/users/index');
		return $viewModel;
	}
	
	public function addAction()
	{
		$form = new Form(include __DIR__.'/../Form/User.php');
		$q = $this->getEntityManager()->createQuery("select partial g.{id,name} from Users\Entity\Usersgroups g");
		$pairGroups = array();
		foreach ($q->getResult() as $group){
			$pairGroups[$group->id] = $group->name;
		}
		$form->get('groups')->setOptions(array('value_options' => $pairGroups));
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$user = new Users();
			
			$form->setData($request->getPost());
			if ($form->isValid()) {
		
				$formData = $form->getData();
				$formData['password'] = md5($formData['password']);
				
				
				$this->populateEntity($user, $formData);
				$this->getEntityManager()->persist($user);
                $this->getEntityManager()->flush();
		
			
				return $this->redirect()->toRoute('admin', array(
						'controller' => 'users',
						'action'     => 'index',
					));
		
			}
		}
		
		$viewParams = array('form' => $form);
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/users/add');
		
		return $viewModel;
	}
	
	public function editAction()
	{
		//$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
		$id = $this->params('id');
        if (!$id) {
            return $this->redirect()->toRoute('admin', array('module'=>'users', 'controller'=>'users', 'action'=>'add'));
        }
        $user = $this->getEntityManager()->find('Users\Entity\Users', $id);
 
        $form = new Form(include __DIR__.'/../Form/User.php');
        $form->setBindOnValidate(false);
        $form->bind($user);
        $form->get('submit')->setAttribute('label', 'Edit');
        
        $q = $this->getEntityManager()->createQuery("select partial g.{id,name} from Users\Entity\Usersgroups g");
        $pairGroups = array();
        foreach ($q->getResult() as $group){
        	$pairGroups[$group->id] = $group->name;
        }
        //\Zend\Debug\Debug::dump($pairGroups); die();
        $form->get('groups')->setOptions(array('value_options' => $pairGroups));
      
        
		$request = $this->getRequest();
		if ($request->isPost()) {
			$user = new Users();
				
			$form->setData($request->getPost());
			if ($form->isValid()) {
				$form->bindValues();
				/* 
				$formData = $form->getData();
				$formData['password'] = md5($formData['password']);
		
		
				$this->populateEntity($user, $formData);
				$this->getEntityManager()->persist($user);
				 */
				
				$this->getEntityManager()->flush();
		
					
				return $this->redirect()->toRoute('admin', array(
						'controller' => 'users',
						'action'     => 'index',
				));
		
			}
		}
		
		$viewParams = array(
			            'id' => $id,
			            'form' => $form,
        			);
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/users/add');
		
		return $viewModel;
	}
	
	public function deleteAction()
	{
		//$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
		$id = $this->params('id');
		if (!$id) {
			// Redirect to list of users
			return $this->redirect()->toRoute('admin', array(
						'controller' => 'users',
						'action'     => 'index',
					));
		}
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->post()->get('del', 'No');
			if ($del == 'Yes') {
				$id = (int)$request->post()->get('id');
				$user = $this->getEntityManager()->find('Users\Entity\Users', $id);
				if ($user) {
					$this->getEntityManager()->remove($user);
					$this->getEntityManager()->flush();
				}
			}
		
			// Redirect to list of users
			return $this->redirect()->toRoute('admin', array(
						'controller' => 'users',
						'action'     => 'index',
					));
		}
		
		$viewParams = array(
						'id' => $id,
						'user' => $this->getEntityManager()->find('Users\Entity\Users', $id)->getArrayCopy()
					);
		$viewModel = new ViewModel($viewParams);
		$viewModel->setTemplate('admin/users/add');
		
		return $viewModel;
	}

	/**
	 * (non-PHPdoc)
	 * @see VSF\Mvc\Controller.AbstractActionController::populateEntity()
	 */
	protected function populateEntity($entity, $data)
	{
		//\Zend\Debug\Debug::dump($data);
		parent::populateEntity($entity, $data);
		$entity->active = 1;
	}
}